package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.RequestPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"角色列表权限","role:list"},logical = Logical.OR)
    public String list(QueryObject queryObject, Model model){
        PageInfo<Role> pageInfo = roleService.selectByQo(queryObject);
        model.addAttribute("pageInfo",pageInfo);
        return "role/list";
    }

    @RequestMapping("input")
    @RequiresPermissions(value = {"角色编辑权限","role:input"},logical = Logical.OR)
    public String input(Long roleId,Model model){
        //角色回显
        Role role = roleService.selectById(roleId);
        model.addAttribute("role",role);

        //查询所有的角色权限
        List<Permission> list = permissionService.selectAll();
        model.addAttribute("roles",list);

        return "role/input";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"角色保存或新增权限","role:saveOrUpdate"},logical = Logical.OR)
    public String saveOrUpdate(Role role,Long[] Ids){
        //保存
        roleService.saveOrUpdate(role,Ids);
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"角色删除权限","role:delete"},logical = Logical.OR)
    public String delete(Long roleId){
        roleService.deleteById(roleId);
        return "redirect:/role/list";
    }
}
